/**
 * 公示管理 Composable
 * 封装公示管理相关逻辑，支持注册公示和变更公示
 */

import { ref, onMounted } from 'vue'
import { ElMessage, type CheckboxValueType } from 'element-plus'
import { setAutoPublicityApi, publishPublicityApi, getAutoPublicityApi } from '@/api/athlete/index'
import { PublicityType, AutoPublicityStatus } from '@/api/athlete/types'

/**
 * 公示管理 hook
 * @param publicityType 公示类型 (1=注册公示, 2=变更公示)
 */
export function usePublicityManagement(publicityType: PublicityType) {
	// 响应式状态
	const loading = ref(false)
	const autoPublish = ref(false) // 自动公示开关状态

	/**
	 * 获取自动公示配置
	 */
	const fetchAutoPublicityConfig = async () => {
		try {
			loading.value = true
			const response = await getAutoPublicityApi(publicityType)

			console.log('gongshi:', response)

			if (response.code === 200 && response.data) {
				autoPublish.value = response.data === AutoPublicityStatus.ENABLED
			}
		} catch (error) {
			console.error('获取自动公示配置失败:', error)
			ElMessage.error('获取自动公示配置失败')
		} finally {
			loading.value = false
		}
	}

	/**
	 * 设置自动公示
	 * @param enabled 是否启用自动公示
	 */
	const setAutoPublicity = async (enabled: boolean) => {
		try {
			loading.value = true
			const autoPublicity = enabled ? AutoPublicityStatus.ENABLED : AutoPublicityStatus.DISABLED

			const response = await setAutoPublicityApi(autoPublicity, publicityType)

			// 检查响应状态码
			if (response.code === 200) {
				autoPublish.value = enabled
				ElMessage.success(`${enabled ? '已启用' : '已关闭'}自动公示`)
			} else {
				// 非200状态码，显示后端返回的警告信息，并恢复原状态
				ElMessage.warning(response.msg || '设置自动公示失败')
				autoPublish.value = !enabled
			}
		} catch (error) {
			console.error('设置自动公示失败:', error)
			ElMessage.error('设置自动公示失败')
			// 失败时恢复原状态
			autoPublish.value = !enabled
		} finally {
			loading.value = false
		}
	}

	/**
	 * 发布公示
	 */
	const publishPublicity = async () => {
		try {
			loading.value = true

			const response = await publishPublicityApi(publicityType)

			// 检查响应状态码
			if (response.code === 200) {
				ElMessage.success('发布公示成功')
			} else {
				// 非200状态码，显示后端返回的警告信息
				ElMessage.warning(response.msg || '发布公示失败')
			}
		} catch (error) {
			console.error('发布公示失败:', error)
			ElMessage.error('发布公示失败')
		} finally {
			loading.value = false
		}
	}

	/**
	 * 处理自动公示开关变化
	 * @param value Element Plus Checkbox 的变化值
	 */
	const handleAutoPublishChange = (value: CheckboxValueType) => {
		// 将值转换为 boolean 类型
		const booleanValue = Boolean(value)
		setAutoPublicity(booleanValue)
	}

	/**
	 * 处理发布公示按钮点击
	 */
	const handlePublishClick = () => {
		publishPublicity()
	}

	// 组件挂载时获取配置
	onMounted(() => {
		fetchAutoPublicityConfig()
	})

	return {
		// 状态
		loading,
		autoPublish,

		// 方法
		fetchAutoPublicityConfig,
		setAutoPublicity,
		publishPublicity,
		handleAutoPublishChange,
		handlePublishClick
	}
}

/**
 * 获取公示类型的中文描述
 * @param type 公示类型
 */
export function getPublicityTypeLabel(type: PublicityType): string {
	switch (type) {
		case PublicityType.REGISTER:
			return '注册公示'
		case PublicityType.CHANGE:
			return '变更公示'
		default:
			return '公示'
	}
}
